package com.michaelbulava.TFS.tasks.workspaces;

import com.intellij.openapi.progress.ProgressIndicator;
import com.michaelbulava.TFS.Messages;
import com.michaelbulava.TFS.tasks.BackgroundableTask;
import com.microsoft.tfs.core.TFSTeamProjectCollection;
import com.microsoft.tfs.core.clients.versioncontrol.VersionControlClient;
import com.microsoft.tfs.core.clients.versioncontrol.WorkspaceLocation;
import com.microsoft.tfs.core.clients.versioncontrol.WorkspaceOptions;
import com.microsoft.tfs.core.clients.versioncontrol.WorkspacePermissionProfile;
import com.microsoft.tfs.core.clients.versioncontrol.soapextensions.WorkingFolder;
import com.microsoft.tfs.core.clients.versioncontrol.soapextensions.Workspace;
import com.microsoft.tfs.util.Check;

import java.text.MessageFormat;

/**
 * Created by mbulava on 7/16/2015.
 */
public class CreateWorkspaceTask extends BackgroundableTask {

    private final VersionControlClient vcClient;
    private final WorkingFolder[] workingFolders;
    private final String workspaceName;
    private final String comment;
    private final WorkspaceLocation location;
    private final WorkspaceOptions options;
    private final WorkspacePermissionProfile permissionProfile;
    private volatile Workspace workspace;

    private TFSTeamProjectCollection connection;

    public CreateWorkspaceTask(TFSTeamProjectCollection connection, WorkingFolder[] workingFolders,
                                  String workspaceName, String comment, WorkspaceLocation location,
                                  WorkspaceOptions options, WorkspacePermissionProfile permissionProfile){
        super(null, getName(workspaceName), false);
        Check.notNull(connection, "connection");
        Check.notNull(workspaceName, "workspaceName");

        this.vcClient = connection.getVersionControlClient();
        this.workingFolders = workingFolders;
        this.workspaceName = workspaceName;
        this.comment = comment;
        this.location = location;
        this.options = options;
        this.permissionProfile = permissionProfile;
        this.connection = connection;
    }

    private static String getName(String workspaceName){
        String messageFormat = Messages.getString("CreateWorkspaceCommand.CommandTextFormat");
        return MessageFormat.format(messageFormat, new Object[]{workspaceName});
    }

    @Override
    protected boolean runInBackground() {
        return false;
    }

    @Override
    public void run(ProgressIndicator progressIndicator) {
        String messageFormat = Messages.getString("CreateWorkspaceCommand.ProgressMonitorTextFormat");
        String message = MessageFormat.format(messageFormat, new Object[] { this.workspaceName });
        progressIndicator.setText(message);
        progressIndicator.setIndeterminate(true);

        this.workspace = this.vcClient.createWorkspace(this.workingFolders, this.workspaceName, this.comment == null ? "" : this.comment, this.location, this.options, this.permissionProfile);

    }

    public Workspace getWorkspace(){
        return this.workspace;
    }
}
